//
//  UserProfileView.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 12.04.25.
//

import SwiftUI

struct UserProfileView: View {
    @Environment(\.presentationMode) var presentationMode
    @Environment(\.presentToast) var presentToast
    @EnvironmentObject var appState: AppState
    
    @StateObject var viewModel = UserProfileViewModel()
    @State var isWxBind = false
    @State var wxState = ""
    @State var isPhoneBind = false
    @State var phoneState = ""
    @State var hasPw = false
    @State var pwState = ""
    @State var nickname = ""
    @State var gender = ""
    
    var body: some View {
        ScrollView {
            VStack {
                NavigationLink(
                    destination: { ProfileEditNameView() },
                    label: { ProfileItemView(itemTitle: Text("昵称"), itemDesc: Text(nickname)) }
                )
                
                NavigationLink(
                    destination: { ProfileEditGenderView() },
                    label: { ProfileItemView(itemTitle: Text("性别"), itemDesc: Text(gender)) }
                )
                // 不要了
                // ProfileItemView(itemTitle: "生日", itemDesc: "2025-03-01")
            }
            .background(Color.koiColorBackgroundItem)
            .cornerRadius(16)
            .padding(.top, 16)
            
            VStack {
                if isPhoneBind {
                    NavigationLink(
                        destination: { PhoneBindNewView() },
                        label: { ProfileItemView(itemTitle: Text("手机号"), itemDesc: Text(phoneState)) }
                    )
                } else {
                    NavigationLink(
                        destination: { PhoneBindView() },
                        label: { ProfileItemView(itemTitle: Text("手机号"), itemDesc: Text(phoneState)) }
                    )
                }
                
                if isWxBind {
                    ProfileItemView(itemTitle: Text("微信"), itemDesc: Text(wxState))
                } else {
                    Button(action: bindWx) {
                        ProfileItemView(itemTitle: Text("微信"), itemDesc: Text(wxState))
                    }
                }
                
                if hasPw {
                    NavigationLink(
                        destination: { PasswordModifyView() },
                        label: { ProfileItemView(itemTitle: Text("密码"), itemDesc: Text(pwState)) }
                    )
                } else {
                    NavigationLink(
                        destination: { PasswordSetView() },
                        label: { ProfileItemView(itemTitle: Text("密码"), itemDesc: Text(pwState)) }
                    )
                }
            }
            .background(Color.koiColorBackgroundItem)
            .cornerRadius(16)
            .padding(.top, 16)
            
            Button(action:logout) {
                Text("退出登录")
                    .font(.system(size: 14))
                    .fontWeight(.medium)
                    .foregroundColor(Color.koiColorPrimary)
                    .padding(.vertical, 18)
                    .frame(maxWidth: .infinity)
                    .background(Color(hex: "EF443A").opacity(0.04))
                    .cornerRadius(16)
                    .padding(.top, 16)
            }
            
            
            NavigationLink(destination: { RemoveAccountView() }, label: {
                Text("注销账号")
                    .font(.system(size: 14))
                    .foregroundColor(Color.koiColorTitleSecond)
                    .padding(.vertical, 25)
            })
        }
        .background(Color.koiColorBackground)
        .padding(.horizontal, 20)
        .koiToolbar(title: Text("个人资料"))
        .onChange(of: viewModel.logoutState) { value in
            if value == "success" {
                appState.isLoggedIn = false
            }
        }
        .onChange(of: viewModel.bindWxState) { value in
            if value == "" {
                return
            }
            if value == "success" {
                wxState = "已绑定"
                presentToast(.init(message: "绑定成功"))
                viewModel.bindWxState = ""
            } else {
                presentToast(.init(message: value))
                viewModel.bindWxState = ""
            }
        }
        .onAppear {
            if let user = KoiAccountingUserManager.standard.user() {
                isWxBind = user.isBindWeChat()
                if user.isBindWeChat() == true {
                    wxState = "已绑定"
                } else {
                    wxState = "未绑定"
                }
                hasPw = user.hasPassword()
                if user.hasPassword() == true {
                    pwState = "已设置"
                } else {
                    pwState = "未设置"
                }
                isPhoneBind = user.isBindPhone()
                if isPhoneBind {
                    phoneState = user.getHidePhoneNumber()
                } else {
                    phoneState = ""
                }
                nickname = user.getNickname()
                gender = user.getGender()
            }
            
            viewModel.getUserProfile()
        }
    }
    
    private func bindWx() {
        viewModel.bindWx()
    }
    
    private func logout() {
        viewModel.logout()
    }
}

struct ProfileItemView : View {
    
    var itemTitle: Text
    var itemDesc: Text
    
    var body: some View {
        HStack {
            itemTitle
                .font(.system(size: 14))
                .fontWeight(.medium)
                .foregroundColor(Color.koiColorTitle)
            
            Spacer()
            
            itemDesc
                .font(.system(size: 13))
                .foregroundColor(Color.koiColorTitleSecond)
            Image("koi_ic_arrow_right")
                .resizable()
                .frame(width: 16, height: 16)
        }
        .padding(EdgeInsets(top: 18, leading: 14, bottom: 18, trailing: 14))
    }

}

#Preview {
    UserProfileView()
}
